﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CarExam.BLL;
using CarExam.DBUtility;
using CarExam.Model;

namespace CarExam
{
    public partial class Frm_login : Form
    {
        UserBLL _userbll = new UserBLL();
        UserInfo _ui = new UserInfo();
        int errornum = 0;

        public Frm_login()
        {
            InitializeComponent();
            //_userbll.UpdateIsLocked(2);
        }

        private void btn_login_Click(object sender, EventArgs e)
        {
            if (errornum >= 3)
            {
                //MessageBox.Show("登录失败次数超过了三次，请重新打开程序进行登录！");
                DialogResult dr = MessageBox.Show("登录失败次数超过了三次，请重新打开程序进行登录！", "提示", MessageBoxButtons.OK);
                if (dr == DialogResult.OK)
                {
                    this.Close();
                }
            }
            else
            {
                #region 验证准考证号和密码

                if (string.IsNullOrEmpty(this.txt_account.Text))
                {

                    MessageBox.Show("准考证号不能为空！");
                    return;
                }
                if (string.IsNullOrEmpty(this.txt_password.Text))
                {

                    MessageBox.Show("密码不能为空！");
                    return;
                }

                #endregion

                int res = _userbll.UserLogin(txt_account.Text, Util.MD5(txt_password.Text), out _ui);
                if (res == 0)
                {
                    Frm_Main frm = new Frm_Main(_ui);
                    frm.Show();
                    this.Hide();
                }
                else if (res == 1)
                {
                    ++errornum;
                    MessageBox.Show("账号/密码错误！");
                }
                else if (res == 2)
                {
                    MessageBox.Show("此账号已经登录，不能重复登录！");
                }
            }
        }

        private void btn_eixt_Click(object sender, EventArgs e)
        {
            //bool res = _userbll.UpdateIsLocked(_ui.Uid);
            Application.Exit();
        }
    }
}
